package hpu.e4_demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import hpu.e4_demo.common.Result;
import hpu.e4_demo.entity.TUserInfo;
import hpu.e4_demo.entity.TUsers;
import hpu.e4_demo.mapper.TUserInfoMapper;
import hpu.e4_demo.mapper.TUsersMapper;
import hpu.e4_demo.service.ITUserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hpu.e4_demo.utils.JwtUtil;
import hpu.e4_demo.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author FFF
 * @since 2025-06-21
 */
@Service
public class TUserInfoServiceImpl extends ServiceImpl<TUserInfoMapper, TUserInfo> implements ITUserInfoService {
    @Autowired
    private TUserInfoMapper tUserInfoMapper;
    @Autowired
    private TUsersMapper tUsersMapper;

    @Override
    public Result<List<TUserInfo>> getAllUserInfo(Integer currentPage, Integer size) {

        IPage<TUserInfo> page = new Page<>(currentPage,size);
        IPage<TUserInfo> result = tUserInfoMapper.findAll(page);
        List<TUserInfo> result3 = result.getRecords();
        return Result.ok(result3);
    }

    @Override
    public Result<String> addUserInfo(TUserInfo tUserInfo) {
        TUsers tUsers = new TUsers();
        tUsers.setUserName(tUserInfo.getUserName());
        tUsers.setRole("用户");
        tUsers.setPassword(MD5.getMD5String(tUserInfo.getPassword()));
        tUsersMapper.insert(tUsers);
        tUserInfo.setPassword(MD5.getMD5String(tUserInfo.getPassword()));
        return tUserInfoMapper.insert(tUserInfo) > 0 ? Result.ok("添加成功") : Result.fail("添加失败");
    }

    @Override
    public Result<String> modifyUserInfo(TUserInfo tUserInfo) {
        tUserInfo.setModifyTime(LocalDateTime.now());

        if (tUserInfo.getPassword() != null){
            tUserInfo.setPassword(MD5.getMD5String(tUserInfo.getPassword()));
            tUsersMapper.updateUserPassword(tUserInfo.getUserName(),tUserInfo.getPassword());
        }
        return tUserInfoMapper.updateById(tUserInfo) > 0 ? Result.ok("修改成功") : Result.fail("修改失败");
    }

    @Override
    public Result<String> deleteUserInfo(String userName) {
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",userName);

        return tUsersMapper.delete(wrapper) > 0 ? Result.ok("删除成功") : Result.fail("删除失败");
    }

    @Override
    public Result<List<TUserInfo>> getById(String userName, String userTureName,String userPhoneNumber) {
        List<TUserInfo> userInfo = tUserInfoMapper.getById(userName, userTureName,userPhoneNumber);
        if (userInfo == null) {
            return Result.fail(userInfo);
        }
        return Result.ok(userInfo);
    }

    @Override
    public Result<String> updateUserPassword(String userName, String oldPassword, String newPassword) {
        oldPassword = MD5.getMD5String(oldPassword);
        newPassword = MD5.getMD5String(newPassword);
        QueryWrapper<TUsers> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",userName).eq("password",oldPassword);
        if (tUsersMapper.selectCount(wrapper) <= 0){
            return Result.fail("修改失败，用户名或原始密码不正确");
        }
        tUserInfoMapper.update(userName,newPassword);
        tUsersMapper.updateUserPassword(userName,newPassword);
        return Result.ok("修改成功");
    }

    @Override
    public Result<String> setRegister(String userName, String role) {

        return tUsersMapper.updateRegister(userName,role) > 0 ? Result.ok("修改成功") : Result.fail("修改失败");
    }


}
